package Sudoku3.rules;

import Sudoku3.structure.*;

public class rule07_common_crossing {
	
	public rule07_common_crossing(blk b, grid g) throws Exception {
		
		//loop values
		for (int v = 1; v < 10; v++) {

			if ((b.getPossible(v).cardinality() < 2) || (b.getPossible(v).cardinality() > 3) ) { continue; }
			
			bucket bucket1 = new bucket();
			
			for(cell c1 : b) {
				if (!(c1.isSolved()) && (c1.isPossible(v))) { bucket1.add(c1); }
			}
		
			//loop rows
			for (int r = 0; r < 9; r++) {
				row row1 = g.getRow(r);
				if (!(b.intersects(row1))) { continue; }
				
				//get common cells
				bucket bucket2 = b.getCommonCells(row1);
				
				if ((bucket2.size() < 2) || (bucket2.size() > 3) ) { continue; }
				
				//continue if the common cells bucket doesn't contain all of the possible cells
				if (!(bucket2.containsAll(bucket1.getCellSet()))) { continue; }
				
				//all cells in the row that are not in the possible bucket can have this value removed
				for (cell a : row1) {
					if ( (!(a.isSolved())) && (!(bucket1.contains(a))) && (a.isPossible(v)) ) {
						a.removePossible(v);
						System.out.println("Rule 07:  Common crossing found in " + b.toString() + " and " + row1.toString() + " for cells(" + bucket1.toStringCells() + ") for value(" + v + ").  Removing this possibility from cell(" + a.toStringCoords() + ").");
					}
				}
			}
		}
		
		//repeat for cols
		for (int v = 1; v < 10; v++) {

			if ((b.getPossible(v).cardinality() < 2) || (b.getPossible(v).cardinality() > 3) ) { continue; }
			
			bucket bucket1 = new bucket();
			
			for(cell c1 : b) {
				if (!(c1.isSolved()) && (c1.isPossible(v))) { bucket1.add(c1); }
			}
		
			//loop cols
			for (int c = 0; c < 9; c++) {
				col col1 = g.getCol(c);
				if (!(b.intersects(col1))) { continue; }
				
				//get common cells
				bucket bucket2 = b.getCommonCells(col1);
				
				if ((bucket2.size() < 2) || (bucket2.size() > 3) ) { continue; }
				
				//continue if the common cells bucket doesn't contain all of the possible cells
				if (!(bucket2.containsAll(bucket1.getCellSet()))) { continue; }
				
				//all cells in the col that are not in the possible bucket can have this value removed
				for (cell a : col1) {
					if ( (!(a.isSolved())) && (!(bucket1.contains(a))) && (a.isPossible(v)) ) {
						a.removePossible(v);
						System.out.println("Rule 07:  Common crossing found in " + b.toString() + " and " + col1.toString() + " for cells(" + bucket1.toStringCells() + ") for value(" + v + ").  Removing this possibility from cell(" + a.toStringCoords() + ").");
					}
				}
			}
		}
		
	
		
		
	}
	
	
	
		
}

		
		
		
		
		
//		//loop through rows
//		for (int i = 0; i < 9; i++) {
//			row r = g.getRow(i);
//
//			if (!(b.intersects(r))) { continue; } //ignore rows that do not cross this blk
//			
//			//iterate values
//			for (int v = 1; v < 10; v++) {
//				bucket z = new bucket();
//				//iterate cells in block
//				for (cell x : b) {
//					if (x.isSolved()) { continue; }
//					if (x.isPossible(v)) { z.add(x); }
//				}
//				//continue to next value if number of cells found is not 2 or 3
//				if ( (z.size() < 2) || (z.size() > 3) ) { continue; }
//			
//				//continue to next value if cells are not in the same row
//				boolean isCommon = true;
//				for (cell y : z) {
//					if (!(y.getR() == r.getID())) { isCommon = false; }
//				}
//				if (!(isCommon)) { continue; }
//				
//				//all cells in the row that are not in the bucket can have this value removed
//				for (cell a : r) {
//					if ( !(a.isSolved()) && !(z.contains(a)) && (a.isPossible(v))) {
//						//a.removePossible(v);
//						System.out.println("Rule 07:  Common crossing found in " + b.toString() + " and " + r.toString() + " for cells(" + z.toStringCells() + ") for value(" + v + ").  Removing this possibility from cell(" + a.toStringCoords() + ").");
//					}
//				}
//			}
//		}
		
//		//repeat for columns
//		for (int i = 0; i < 9; i++) {
//			col c = g.getCol(i);
//
//			if (!(b.intersects(c))) { continue; } //ignore cols that do not cross this blk
//			
//			//iterate values
//			for (int v = 1; v < 10; v++) {
//				bucket z = new bucket();
//				//iterate cells in block
//				for (cell x : b) {
//					if (x.isSolved()) { continue; }
//					if (x.isPossible(v)) { z.add(x); }
//				}
//				//continue to next value if number of cells found is not 2 or 3
//				if ( (z.size() < 2) || (z.size() > 3) ) { continue; }
//			
//				//continue to next value if cells are not in the same col
//				boolean isCommon = true;
//				for (cell y : z) {
//					if (!(y.getC() == c.getID())) { isCommon = false; }
//				}
//				if (!(isCommon)) { continue; }
//				
//				//all cells in the col that are not in the bucket can have this value removed
//				for (cell a : c) {
//					if ( !(a.isSolved()) && !(z.contains(a)) && (a.isPossible(v))) {
//						a.removePossible(v);
//						System.out.println("Rule 07:  Common crossing found in " + b.toString() + " and " + c.toString() + " for cells(" + z.toStringCells() + ") for value(" + v + ").  Removing this possibility from cell(" + a.toStringCoords() + ").");
//					}
//				}
//			}
//		}
//	}
//
//}
